package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domain.Person;

/**
 * Servlet implementation class GetTrafficController
 */
public class GetTrafficController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetTrafficController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if(isLogin(request, response)){
			String scheduleId = request.getParameter("scheduleId");
			
			request.setAttribute("scheduleId", scheduleId);
			
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/getTraffic.jsp");
			dispatcher.forward(request,response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	private boolean isLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		boolean isLogin = false;
		HttpSession session = request.getSession(true);
		Person user = (Person) session.getAttribute("logedinUser");
		if(user!=null){
			isLogin = true; 
			request.setAttribute("currentUser", user);
		}else{// have log in yet
			isLogin = false;
			System.out.println("User has not loged in yet!!");
			request.setAttribute("message", "Please log in first!!");
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/login.jsp");
			dispatcher.forward(request,response);
		}
		
		return isLogin;
	}
}
